#File Name: TransparencyBetasExport.r
#Author: James Hollyer
#Date: 02/28/2013
#Purpose: To extract the discrimination and difficulty parameter from an
#IRT analysis on the missingness of WDI data in BUGS.
#Data Input: TransparencyCoefficients.Rdata 
#OS: Windows 7 and Windows XP

setwd("c:/users/james/desktop/dropbox/transparency_and_democracy/transparencyindex/PAReplicationMaterials/Index Properties/")


library(arm)
library(foreign)
library(rjags)
library(R2jags)

load("TransparencyIndex2013.RData")

attach.jags(results)

discrimination<-c(1:240)

for(n in 1:240){
  eval(parse(text=sprintf("discrimination[%s] <- mean(beta%s[,2])", n, n)))
}

discriminationub<-c(1:240)
discriminationlb<-c(1:240)

discriminationHPD<-cbind(discriminationub, discriminationlb)

for(n in 1:240){
  eval(parse(text=sprintf("discriminationHPD[%s,] <- HPDinterval(as.mcmc(as.matrix(beta%s[,2])))",n,n)))
}

difficulty <- c(1:240)

for(n in 1:240){
  eval(parse(text=sprintf("difficulty[%s] <- mean(beta%s[,1])", n, n)))
}

difficultylb<-c(1:240)
difficultyub<-c(1:240)

difficultyHPD <-cbind(difficultylb, difficultyub)

for(n in 1:240){
  eval(parse(text=sprintf("difficultyHPD[%s,] <- HPDinterval(as.mcmc(as.matrix(beta%s[,1])))",n,n)))
}

TransparencyCoefficients.data <- data.frame(discrimination, discriminationHPD, difficulty, difficultyHPD)
write.dta(TransparencyCoefficients.data, file="TransparencyCoefficients112313.dta")

save.image("TransparencyCoefficients112313")